package mysql

import (
	"gitee.com/captials-team/ubdframe/src/common"
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	mysql2 "github.com/go-sql-driver/mysql"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"time"
)

func NewGormDB(c configstc.DBConfig) *gorm.DB {
	sqlDB, err := NewConn(c, func(config *mysql2.Config) {
		config.Loc = time.Local
	})
	common.ErrPanic(err)

	gormDB, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB}), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			//定义结构体的TableName方法里无法使用传值进行动态表明设置
			//这里可以做统一的表名前缀
			TablePrefix:         c.TablePrefix,
			SingularTable:       true,
			NameReplacer:        nil,
			NoLowerCase:         false,
			IdentifierMaxLength: 0,
		},
		DisableForeignKeyConstraintWhenMigrating: true,
		SkipDefaultTransaction:                   true, //默认不经过事务
	})
	common.ErrPanic(err)

	return gormDB
}
